#include <stdio.h>
#include <pthread.h>
#include <stdbool.h>

static int a = 0;
pthread_mutex_t mutex;

void* calc(void* arg) {
    bool isadd = *(bool*)arg;
    if (isadd) {
        for (int i = 0; i < 100000; i++) {
            // pthread_mutex_lock(&mutex);
            a++;
            // pthread_mutex_unlock(&mutex);
        }
    } else {
        printf("a = %d\n", a);
    }
    pthread_exit(NULL);
}

int main() {
    pthread_t tid1, tid2;
    bool isadd = true;
    pthread_mutex_init(&mutex, NULL);

    pthread_create(&tid1, NULL, calc, &isadd);
    pthread_create(&tid2, NULL, calc, &isadd);

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    pthread_mutex_destroy(&mutex);
    
    isadd = false;
    calc(&isadd);

    return 0;
}
